package com.hxzy.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.hxzy.common.enums.AckCode;
import com.hxzy.controller.login.req.LoginReq;
import com.hxzy.controller.user.req.UserReq;
import com.hxzy.controller.user.resp.UserResp;
import com.hxzy.entity.UserinfoWy;
import com.hxzy.exception.ServiceException;
import com.hxzy.mapper.UserinfoWyMapper;
import com.hxzy.service.UserinfoWyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Desc 描述
 * @Author: admin
 * @Date: 2023/08/15/11:43
 */
@Service
public class UserinfoWyServiceImpl implements UserinfoWyService{

    @Autowired
    private UserinfoWyMapper  userinfoWyMapper;


    @Override
    public boolean insert(UserinfoWy record) {
        return this.userinfoWyMapper.insert(record)>0;
    }


    @Override
    public UserinfoWy selectByPrimaryKey(Long id) {
        return this.userinfoWyMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateByPrimaryKeySelective(UserinfoWy record) {
        return this.userinfoWyMapper.updateByPrimaryKeySelective(record)>0;
    }

    @Override
    public boolean updateByPrimaryKey(UserinfoWy record) {
        return this.userinfoWyMapper.updateByPrimaryKey(record)>0;
    }

    @Override
    public boolean deleteById(Long id) {
        UserinfoWy  userinfoWy=new UserinfoWy();
        userinfoWy.setId(id);
        userinfoWy.setStatus("1");
        userinfoWy.setToken("");
        return this.userinfoWyMapper.updateByPrimaryKeySelective(userinfoWy)>0;
    }

    /**
     * 登录
     * 1、如果用户名有相同的，返回提交，必须要手机号加入认证
     * 2、如果成功，生成令牌，令牌存放到数据库里面，设定一个过期时间。进行维护
     * @param req
     * @return
     */
    @Override
    public UserinfoWy login(LoginReq req) {
        long count=this.userinfoWyMapper.selectByNameCount(req.getAccount());
        if(count==0){
            throw new ServiceException(AckCode.USERNAME_PASSWORD_ERROR);
        }else if(count>1){
            //如果手机号码是空的
            if(StrUtil.isBlank(req.getMobile())) {
                //有同名用户名
                throw new ServiceException(AckCode.USERNAME_EXISTS_MOBILE);
            }
        }

        //查询数据库了
        UserinfoWy db=this.userinfoWyMapper.searchByCondition(req);
        if(db==null){
            throw new ServiceException(AckCode.MOBILE_ERROR);
        }

        //判断密码对不对
        if(!BCrypt.checkpw(req.getPassword(), db.getPassword())){
            throw new ServiceException(AckCode.USERNAME_PASSWORD_ERROR);
        }

        if(db.getStatus().equals("1")){
            throw new ServiceException(AckCode.USER_BANNED);
        }

        //生成令牌，写入到数据库
        db.setToken(UUID.randomUUID().toString());
        //令牌过期时间60
        DateTime expiredDate = DateUtil.offsetMinute(new Date(), 60);
        db.setTokenExpired(expiredDate);
        //更新数据库
        int resultCount = this.userinfoWyMapper.updateByPrimaryKeySelective(db);
        if(resultCount==0){
            throw new ServiceException(AckCode.USER_UPDATE_TOKEN_ERROR);
        }
        return db;
    }

    @Override
    public UserinfoWy searchByToken(String uuid) {

        return this.userinfoWyMapper.searchByToken(uuid);
    }

    @Override
    public void logout(String token) {
        this.userinfoWyMapper.logout(token);
    }

    @Override
    public PageInfo<UserResp> searchPage(UserReq req) {
        return PageHelper.startPage(req.getPage(),req.getSize()).doSelectPageInfo(()->{
            //调用数据访问层
            userinfoWyMapper.searchPage(req);
        });
    }

    @Override
    public int existsMobile(String mobile) {
        return this.userinfoWyMapper.existsMobile(mobile);
    }

    /**
     * 逻辑删除
     * @param ids
     * @return
     */
    @Override
    public boolean deleteBatch(Long[] ids) {
        //数据不大，没有问题(10笔数据，向数据发送10条更新语句)，如果数据大，效率超级慢
//        for(Long id : ids){
//            UserinfoWy  userinfoWy=new UserinfoWy();
//            userinfoWy.setId(id);
//            userinfoWy.setStatus("1");
//            this.userinfoWyMapper.updateByPrimaryKeySelective(userinfoWy);
//        }
        return this.userinfoWyMapper.updateBatchStatus(ids,"1")>0;
    }
}
